package com.hutao.medical.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hutao.medical.common.config.ConfigProperties;
import com.hutao.medical.common.util.DateUtil;
import com.hutao.medical.common.util.Result;
import com.hutao.medical.common.util.StringUtil;

@RequestMapping("/upload")
@Controller
public class FileUploadController {
	@Autowired
	private ConfigProperties configProperties;

	@ResponseBody
	@RequestMapping("/uploadLocalImg")
	public Map<String,Object> uploadLocalImg(HttpServletRequest request,HttpServletResponse response ){
		Map<String,Object> map = new HashMap<>();
		try {
		//String fileName = request.getHeader("-fileName");
		String fileName=new Date().getTime()+"";
		String fileType = request.getHeader("-fileType");
		String firstGuid = request.getHeader("-firstGuid");
		String folderUrl= new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
		String tempFilePath=configProperties.getServer().getCredentialFileDir()+firstGuid+"/"+folderUrl;
		
		int contentLength = request.getContentLength();
		int maxFileSize = 1024*1024*2;
		if(contentLength>maxFileSize){
			map.put("ret", "tooMax");
			return map;
		}
		
		String imagePath =tempFilePath+fileName+"."+fileType;
		System.out.println("- 上传路径=>"+imagePath);
		int RLen = 1024*4;
		byte[] ReadByte = new byte[RLen];//输入流缓冲区		
		int ReadCount = 0;//记录从输入流中每次实际读取的字节数
		
		ServletInputStream input = request.getInputStream();//取得输入流
		
		File newFileSavePath = new File(new File(imagePath).getParent());
		if (!newFileSavePath.exists()){
			newFileSavePath.mkdirs();
		}
		BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(imagePath));
		while ((ReadCount=input.readLine(ReadByte,0,RLen)) > -1){
			outStream.write(ReadByte,0,ReadCount);
		}
		outStream.flush();
		
		input.close();
		outStream.close();
		ReadByte = null;
		map.put("ret", "success");
		map.put("fileName", fileName+"."+fileType);
		map.put("fileUrl", firstGuid+"/"+folderUrl);
		} catch (Exception e) {
			map.put("ret", "exception");
		}
		
		return map;
	}
	
	@PostMapping(value = "/tempUploadFile")
	@ResponseBody
	public Result tempUploadFile(MultipartFile[] files, HttpServletRequest request) {
		String fileName = "";
		String filePaht = "";
		
		try {
			for (MultipartFile file : files) {
				
			
			if (null != file) {
				Map<String, Object> map = new HashMap();
				fileName = file.getOriginalFilename();
				String fileSuffixes = fileName.substring(fileName.lastIndexOf("."));
				String filePath = StringUtil.getId() + fileSuffixes;
				String savePath = configProperties.getServer().getPrintRegisterUploadPic();
				String currentDateStr = DateUtil.dateFormat(new Date());
				File savePathFile = new File(savePath + "/temp/" + currentDateStr);
				
				if  (!savePathFile.exists()) {
					savePathFile.mkdirs();
				}
				
				file.transferTo(new File(savePath + "/temp/" + currentDateStr + "/" + filePath));
				String picHostPath = configProperties.getServer().getPicHostPath();
				
				String printRegisterUploadPicPath = configProperties.getServer().getPrintRegisterUploadPicPath();
				map.put("printRegisterUploadPicPath", printRegisterUploadPicPath);
				map.put("filePath", "temp/" + currentDateStr + "/" + filePath);
				
				return new Result(true, "上传成功!", map, 200);
			}
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return new Result(false, "上传失败!", null, 500);
	}
	
	
}



